#include "stdafx.h"
#include "TestPersisterFacadeImp.h"
#include "PersisterFacadeImp.h"
#include "Tree.h"
#include "PersistRegBase.h"
#include "PDMSPerObjManager.h"

using namespace PDMS::PDMSDBIF;
using namespace PDMS::PDMSFrame;

CPPUNIT_TEST_SUITE_REGISTRATION( TestPersisterFacadeImp );


TestPersisterFacadeImp::TestPersisterFacadeImp()
{
}


TestPersisterFacadeImp::~TestPersisterFacadeImp()
{
}

void TestPersisterFacadeImp::TestRegister()
{
	PersisterFacadePtr ptr;
	UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
							 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( ptr != NULL );

	es = PersisterFacade::CreateInstance(
							 GET_PERSISTER_FACADE_ID( eDESIGN, (Version)2 ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );
	CPPUNIT_ASSERT( es == UESoft::eObjectNotExist );
	CPPUNIT_ASSERT( ptr == NULL );
}

void TestPersisterFacadeImp::TestOpen()
{
	PersisterFacadePtr ptr;
	UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
							 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );

	es = ptr->Open();
	CPPUNIT_ASSERT( es == UESoft::eOk );

	es = ptr->Open();
	CPPUNIT_ASSERT( es == UESoft::ePersisterFacadeIsOpen );

	ptr->Close();
	
}

void TestPersisterFacadeImp::TestClose()
{
	PersisterFacadePtr ptr;
	UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
							 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );
	
	es = ptr->Close();
	CPPUNIT_ASSERT( es == UESoft::ePersisterFacdeIsNotOpen );

	es = ptr->Open();
	
	es = ptr->Close();
	CPPUNIT_ASSERT( es == UESoft::eOk );
		
}

class TestPFObject : public UeDbObject
{
	UE_PDMSCLASS_DEFINE( TestPFObject );
public:
	TestPFObject(){}
	virtual ~TestPFObject(){}

public:
	virtual Persistable* Clone()
	{
		return NULL;
	}
	virtual BOOL IsLegalParent( UeDbObject* pParent )
	{
		return TRUE;
	}
protected:
	

private:
};

UE_PDMSCLASS_IMPLEMENT( TestPFObject, UeDbObject, UE_PDMSCLASS_ID( TPFO ) );

class TestPFPersisterIF : public PersistRegBase
{
	DECLARE_REGISTER_MAP( TestPFPersisterIF );
public:
	TestPFPersisterIF(){;}
	virtual ~TestPFPersisterIF(){}

protected:
	virtual UESoft::ErrorStatus Retrieve( Persistable*& pObj ){
		return UESoft::eOk;
	}
	virtual UESoft::ErrorStatus Create( Persistable* pObj ){
		return UESoft::eOk;
	}
	virtual UESoft::ErrorStatus Update( Persistable* pObj ){
		return UESoft::eOk;
	}
	virtual UESoft::ErrorStatus Delete( Persistable* pObj ){
		return UESoft::eOk;
	}

private:
};

BEGIN_REGISTER_MAP( TestPFPersisterIF, PersistRegBase )
REGISTER( UE_RUNTIME_CLASS( TestPFObject), GET_REGISTER( PersistRegBase, VersionOne ) )
END_REGISTER_MAP();

void TestPersisterFacadeImp::TestRead()
{
	PersisterFacadePtr ptr;
	UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
							 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );
	
	TestPFObject obj;
	PDMSPerObjManager manager;
	es = ptr->Open();
	CPPUNIT_ASSERT( es == UESoft::eOk );
	es = ptr->ReadDBInfo( &manager );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	es = ptr->Close();
	CPPUNIT_ASSERT( es == UESoft::eOk );
	
}

void TestPersisterFacadeImp::TestSave()
{
	PersisterFacadePtr ptr;
	UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
							 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
							 _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb"), ptr );
	
	TestPFObject obj;
	ptr->Open();
	es = ptr->Save( &obj );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	ptr->Close();
}